/*
 Copyright (C) 2010 Chris Hawkins. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 
 * Neither the name of the author nor the names of its contributors may be used
 to endorse or promote products derived from this software without specific
 prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "ConfigProvData.h"
#import "AsdBatchPutAttributes.h"
#import "DataSelect.h"

@implementation TASDConfigProvDataDomain
- (id)              init {
    DASDDictionary *dictDomains   = [[DASDDomain dictionary] retain];
    DASDDomain     *defaultDomain = [[dictDomains objectForCodeInt: eDASDDomain_Empty] retain];
    self = [super initWithKey: @"DataDomain" 
                      default: [defaultDomain entryName] 
                        title: @"Domain" 
                      details: @"Selected Domain Name"];
    if ( self != nil ) {
        self.choices = [dictDomains dictionaryByName];
    }
    [dictDomains   release];
    [defaultDomain release];
    return self;
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    DASDDictionary *dictDomains   = [[DASDDomain dictionary] retain];
    DASDDomain     *domain        = [[dictDomains objectForName: valueKey_] retain];
    [[cell_ textLabel]       setText: [domain entryName]        ];
    [[cell_ detailTextLabel] setText: [domain entryDescription] ];
    [dictDomains release];
    [domain      release];
}
@end

@implementation TASDConfigProvDataDomainAttribute
@synthesize domain;
- (id)              initWithDomain: (DASDDomain*) domain_ {
    DASDAttribute *allAttribute = (DASDAttribute*) [[DASDAttribute dictionary] objectForCodeInt: eDASDAttribute_ALL];
    self = [super initWithKey: [NSString stringWithFormat: @"DataDomainAttribute_%@", [domain_ entryName]]
                      default: [allAttribute entryName]
                        title: @"Attribute or All" 
                      details: @"Optional selected Attribute (or All)"];
    if ( self != nil ) {
        domain  = [domain_ retain];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 10];
        [dict setObject: allAttribute forKey: [allAttribute entryName]];
        for( DASDAttribute *attribute in [domain valueAttributes] ) {
            [dict setObject: attribute forKey: [attribute entryName]];
        }
        self.choices = dict;
        [dict release];
    }
    return self;
}
- (void)            dealloc {
    [domain     release];
    [super dealloc];
}
- (void)            populateMasterCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: promptTitle ];
    [[cell_ detailTextLabel] setText: valueKey_];
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: valueKey_];
    DASDAttribute *attribute = [[self choices] objectForKey: valueKey_];
    [[cell_ detailTextLabel] setText: [attribute entryDescription]];
}
@end

@implementation TASDConfigProvDataDomainItem
@synthesize domain;
- (id)              initWithDomain: (DASDDomain*) domain_ {
    domain  = [domain_ retain];
    self = [super initWithKey: [NSString stringWithFormat: @"DataDomainItem_%@", [domain entryName]]
                      default: @"0"
                        title: @"Item" 
                      details: @"Selected Domain Item"];
    if ( self != nil ) {
        self.choices = [domain itemNameDictionary];
    }
    return self;
}
- (void)            dealloc {
    [domain     release];
    [super dealloc];
}
- (void)            populateMasterCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: promptTitle ];
    [[cell_ detailTextLabel] setText: [[self choices] objectForKey: valueKey_]];
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: [[self choices] objectForKey: valueKey_]   ];
    NSArray *attributes = [domain valueAttributes];
    if ( attributes == nil || ( 0 == [attributes count] )) {
        [[cell_ detailTextLabel] setText: [NSString string] ];        
    } else {
        [[cell_ detailTextLabel] setText: [domain valueForRow: [valueKey_ integerValue] valueIndex: 0]];
    }
}
@end

@implementation TASDConfigProvDataDomainItemCount
@synthesize domain;
- (id)              initWithDomain: (DASDDomain*) domain_ {
    static const NSString *defaultCount = @"Minimum";
    NSUInteger maxCount = ( [domain_ itemCount] < ASDRqBatchPutAttributes_ItemNumberMax ? [domain_ itemCount] : ASDRqBatchPutAttributes_ItemNumberMax );
    self = [super initWithKey: [NSString stringWithFormat: @"DataDomainItemCount_%@", [domain_ entryName]]
                      default: (NSString*) defaultCount
                        title: [NSString stringWithFormat: @"ItemCount max %u",maxCount] 
                      details: @"Number of Item"];
    if ( self != nil ) {
        domain  = [domain_ retain];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 10];
        if ( 1 <= maxCount ) {
            [dict setObject: @"1"                                     forKey: defaultCount];            
        }
        [dict setObject: [NSString stringWithFormat: @"%u",maxCount]  forKey: @"Maximum"];
        for ( NSUInteger count = 2; count < maxCount; count++ ) {
            [dict setObject: [NSString stringWithFormat: @"%u",count] forKey: [NSString stringWithFormat: @"%u Items", count] ];
            if ( 10 < count ) {
                count += 4;
                continue;
            }
            if ( 4 < count ) {
                count += 2;
                continue;
            }
        }
        self.choices = dict;
        [dict release];
    }
    return self;
}
- (void)            dealloc {
    [domain     release];
    [super dealloc];
}
- (void)            populateMasterCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: promptTitle ];
    [[cell_ detailTextLabel] setText: [[self choices] objectForKey: valueKey_]];
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    [[cell_ textLabel]       setText: valueKey_];
    [[cell_ detailTextLabel] setText: [[self choices] objectForKey: valueKey_]   ];
}
@end

@implementation TASDConfigProvDataSelect
- (id)              init {
    DASDDictionary *dictSelect    = [[DASDSelect dictionary] retain];
    DASDSelect     *defaultSelect = [[dictSelect objectForCodeInt: eDASDSelect_All] retain];
    self = [super initWithKey: @"DataSelect" 
                      default: [defaultSelect entryName] 
                        title: @"Select" 
                      details: @"Select Query"];
    if ( self != nil ) {
        self.choices = [dictSelect dictionaryByName];
    }
    [dictSelect    release];
    [defaultSelect release];
    return self;
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    DASDDictionary *dictSelect   = [[DASDSelect dictionary] retain];
    DASDSelect     *select       = [[dictSelect objectForName: valueKey_] retain];
    [[cell_ textLabel]       setText: [select entryName]        ];
    [[cell_ detailTextLabel] setText: [select entryDescription] ];
    [dictSelect release];
    [select     release];
}
@end
